package com.example.demo.service.impl;

import com.example.demo.entity.Personnelmanagefrequency;
import com.example.demo.handle.FrequencyListParseHandler;
import com.example.demo.mapper.PersonnelmanagefrequencyMapper;
import com.example.demo.service.PersonnelmanagefrequencyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xml.sax.SAXException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hys
 * @since 2021-07-22
 */
@Service
public class PersonnelmanagefrequencyServiceImpl extends ServiceImpl<PersonnelmanagefrequencyMapper, Personnelmanagefrequency> implements PersonnelmanagefrequencyService {

    @Autowired
    private PersonnelmanagefrequencyMapper personnelmanagefrequencyMapper;

    @Override
    public Integer insertFrequencyData(String frequencyXml) {
        //1.获取一个SAXParserFactory的实例对象
        SAXParserFactory factory = SAXParserFactory.newInstance();
        //2.通过factory的newSAXParser()方法获取一个SAXParser类的对象。
        try {
            SAXParser parser = null;
            try {
                parser = factory.newSAXParser();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            } catch (SAXException e) {
                e.printStackTrace();
            }
            //创建SAXParserHandler对象
            FrequencyListParseHandler frequencyListParseHandler = new FrequencyListParseHandler();
            try {
                // 读取xml文件
                parser.parse(frequencyXml, frequencyListParseHandler);
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("~~~~~共有" + frequencyListParseHandler.getPersonnelmanagefrequencyArrayList().size() + "个节点被存储");
            for (Personnelmanagefrequency personnelmanagefrequency : frequencyListParseHandler.getPersonnelmanagefrequencyArrayList()) {
                System.out.println(personnelmanagefrequency.getCsMineCode());
                System.out.println(personnelmanagefrequency.getCsDataTime());
                System.out.println(personnelmanagefrequency.getPsClassCode());
                System.out.println(personnelmanagefrequency.getPsClassName());
                System.out.println(personnelmanagefrequency.getPsClassStime());
                System.out.println(personnelmanagefrequency.getPsClassEtime());
                System.out.println("----finish----");
                personnelmanagefrequencyMapper.insertFrequencyData(personnelmanagefrequency.getCsMineCode(),personnelmanagefrequency.getCsDataTime(),personnelmanagefrequency.getPsClassCode(),personnelmanagefrequency.getPsClassName(),personnelmanagefrequency.getPsClassStime(),personnelmanagefrequency.getPsClassEtime());
            }
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return 1;
    }
}
